#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <unistd.h>

int main()
{
    std::ifstream ifs("test.html");
    assert(ifs.is_open());

    std::string s;
    std::string line;
    while (getline(ifs, line))
    {
        s += line;
    }

    bool is_suffix = true;
    int pos = 0;

    std::vector<std::pair<std::string, std::string>> kvs;
    std::pair<std::string, std::string> kv;

    while (true)
    {
        int pos = s.find("<td", pos);
        if (pos == std::string::npos)
        {
            break;
        }

        int begin = s.find('>', pos);
        int end   = s.find("</td>", begin);
        ++begin;

        if (is_suffix)
        {
            kv.first = s.substr(begin, end - begin);
            is_suffix = false;
        }
        else // if (is_cotype) 
        {
            kv.second = s.substr(begin, end - begin);
            is_suffix = true;
            kvs.push_back({kv.first, kv.second});
        }

        pos = end;
    }
    
    std::ofstream ofs("test.conf");
    assert(ofs.is_open());
    
    for (auto& kv : kvs)
    {
        // std::cout << kv.first << ": " << kv.second << std::endl;
        // sleep(1);
        ofs << kv.first << ' ' << kv.second << std::endl;
    }

    return 0;
}
